<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%--
  Created by IntelliJ IDEA.
  User: qupen
  Date: 2016/10/26
  Time: 9:38
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="../include/head.jsp">
    <jsp:param name="title" value="新增订阅"/>
</jsp:include>

<jsp:include page="../include/admin_header.jsp"/>

<jsp:include page="../include/admin_sidebar.jsp"/>
<div class="content-wrapper content-wrapper-bg">
    <section class="content-header" style="margin-bottom: 20px">
        <h4 class="page-title" style="display: inline-block"><spring:message code="label.add.subscribe"/></h4>
        <input type="text" placeholder="<spring:message code="label.tenant.please.enter.mail.address"/>" class="form-control" id="mail" onblur="verifyEmail('<spring:message code="label.tenant.please.enter.mail.address"/>','<spring:message code="label.tenant.please.enter.available.mail"/>')"
               style="border-radius:5px;border-color:#aabfcb;display:inline-block; width:60%;margin-left: 5%"
               value="${email==null||email=='null'?"":email}">
        <span id="mailTips"></span>
    </section>
    <section class="content">
        <div class="box box-primary table-header-line">
            <div class="box-body" style="padding: 0">
                <table style="width: 100%;" id="table">
                    <thead>
                    <tr>
                        <td class="table-header" style="width: 15%;"><spring:message code="label.choice"/></td>
                        <td class="table-header" style="width: 25%;"><spring:message code="label.event.name"/></td>
                        <td class="table-header" style="width: 25%;"><spring:message code="label.event.type"/></td>
                        <td class="table-header" style="width: 35%;"><spring:message code="label.tenant.create.description"/></td>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
                <div style="text-align: center;margin-top:70px;margin-bottom: 40px">
                    <a id="sure" class="solid-button-blue" href="javascript:;"
                       style="margin-right: 30px"><spring:message code="label.save"/></a>
                </div>
            </div>
        </div>
        <%--</div>--%>
    </section>
</div>
<jsp:include page="../include/foot.jsp"/>
<script>
    $(document).ready(function () {
        $.ajax({
            url: "/admin/api/v1/event/isSubscription",
            type: "get",
            contentType: "application/json;charset=utf-8",
            success: function (data) {
                $.each(data.data, function (index, event) {
                    var $tr = $("<tr></tr>");
                    var $checkbox;
                    if (event.isChoose == 1) {
                        $checkbox = $('<div class="sie-flex-box" style="display: table"></div>')
                                .append($('<div class="item-access item-checked"></div>').attr("data-value", event.event_id)
                                        .append($('<i class="sie-checkbox checked"></i>')));
                    } else {
                        $checkbox = $('<div class="sie-flex-box" style="display: table"></div>').append($('<div class="item-access"></div>').attr("data-value", event.event_id)
                                .append($('<i class="sie-checkbox checked"></i>')));
                    }
                    $tr.append(
                            $("<td></td>").addClass("table-body").attr("style", "width:15%;").append(
                                    $checkbox)).append(
                            $("<td></td>").addClass("table-body").attr("style", "width:25%;").text(event.event_name)).append(
                            $("<td></td>").addClass("table-body").attr("style", "width:25%;").text(event.event_type)).append(
                            $("<td></td>").addClass("table-body").attr("style", "width:35%;").text(event.event_desc));
                    $("tbody").append($tr);
                });
                chooseEvent();
            },
            error: function () {

            }
        });

        $("#sure").click(function () {
            var email = $("#mail").val().trim();
            if (email == null || email == "" || email == undefined) {
                swal("Error", '<spring:message code="label.tenant.please.enter.mail.address"/>', "error");
                return;
            }
            if ($("#mailTips").attr('class') == "no") {
                swal('<spring:message code="label.tenant.mail.not.available"/>', "", "error");
                return;
            }

            var eventIdArr = [];
            $.each($('.item-checked'), function (index, checked) {
                eventIdArr.push($(checked).data("value"))
            });

            $.ajax({
                type: "post",
                url: "/admin/api/v1/event/subscription",
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify({
                    eventArr: eventIdArr,
                    email: email
                }),
                success: function (data) {
                    swal("Success", '<spring:message code="label.subscription.success"/>', 'success');
                    window.location.reload();
                },
                error: function (e) {
                    console.log(e);
                }
            })
        })
    });

    function chooseEvent() {
        $('.item-access').bind('click', function () {
            $(this).hasClass('item-checked') ? $(this).removeClass('item-checked') : $(this).addClass('item-checked');
        });
    }
</script>
